package com.example.gkcircle;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.avos.avoscloud.AVOSCloud;
import com.example.gkcircle.bean.Comment;
import com.example.gkcircle.bean.Lost;
import com.example.gkcircle.bean.Love;
import com.example.gkcircle.bean.Movie;
import com.example.gkcircle.bean.Wish;
import com.example.gkcircle.library.object.Book;
import com.example.gkcircle.library.object.DetailBook;
import com.example.gkcircle.utils.ACache;
import com.example.gkcircle.utils.UniversalImageLoader;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import java.util.List;
import java.util.Map;

/**
 * @author ZhouHua
 * @date 2015年9月4日 下午5:31:30
 */
public class App extends Application {


	private static String officeCookie;
	private static String libraryCookie;
	private static ACache mACache;
	private String AppId="4e5c6bG5E9WslyHyHeMLVBH6-gzGzoHsz";
	private String AppKey="rrb2uKwLLszqlmQqVyATOWYV";
	private static RequestQueue mHttpQueue;
	public static int screenWidth;
	public static int screenHeight;

	public static List<Wish> mHomeList;
	public static List<Lost> mLostList;
	public static List<Lost> mFoundList;
	public static List<Lost> mSearchLostList;
	public static List<Lost> mSearchFoundList;
	public static List<Movie> mMovieGirlList;
	public static List<Movie> mMovieBoyList;
	public static List<Love> mLoveList;
	public static List<Wish> mWishList;
	public static List<Book> mSearchBookList;
	public static List<Book> mBorrowingList;
	public static List<Book> mBorrowedList;
	public static Map<Integer,DetailBook> mDetailBookMap;
	public static Map<String,List<Comment>> mCommentListMap;

	@Override
	public void onCreate() {
		super.onCreate();
		mHttpQueue = Volley.newRequestQueue(this);
		mACache=ACache.get(this);
		AVOSCloud.initialize(this, AppId, AppKey);

		//universal image loader config init
		if (Config.DEVELOPER_MODE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
			StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyDialog().build());
			StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build());
		}
		initImageLoader(getApplicationContext());
	}

	public static RequestQueue getmHttpQueue(){
		return mHttpQueue;
	}


	//内存不足时调用
	@Override
	public void onLowMemory() {
		super.onLowMemory();
		ImageLoader.getInstance().clearMemoryCache();//

	}

	public static String getCookie(int type){
		switch (type){
			case 0:
				return mACache.getAsString("officeCookie");
			case 1:
				return mACache.getAsString("libraryCookie");
			default:
				return null;
		}
	}

	public static void setCookie(int type,String cookie){
		switch (type){
			case 0:
				mACache.put("officeCookie", cookie,1 * ACache.TIME_HOUR);
				break;
			case 1:
				mACache.put("libraryCookie", cookie,1 * ACache.TIME_HOUR);
				break;
		}
	}


	//universal imageloader

	// 配置
	public static class Config {
		public static final boolean DEVELOPER_MODE = false;
	}

	public static void initImageLoader(Context context) {
		// This configuration tuning is custom. You can tune every option, you
		// may tune some of them,
		// or you can create default configuration by
		// ImageLoaderConfiguration.createDefault(this);
		// method.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).defaultDisplayImageOptions(UniversalImageLoader.getPhotorOptions()).threadPriority(Thread.NORM_PRIORITY - 2).denyCacheImageMultipleSizesInMemory().discCacheFileNameGenerator(new Md5FileNameGenerator()).tasksProcessingOrder(QueueProcessingType.LIFO).writeDebugLogs() // Remove
				// for
				// release
				// app
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
	}

}
